package com.example.application.cities;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.application.R;
import com.example.application.task.WeatherTask3;

import java.util.HashMap;
import java.util.List;

public class ThirdFragment extends Fragment {
    Handler handler;
    private ProgressBar bar;
    List<HashMap<String, String>> list;
    SimpleAdapter listItemAdapter;
    public ThirdFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_ins_list, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        GridView mylist = getView().findViewById(R.id.mylistview);
        bar=getView().findViewById(R.id.progressBar);
        mylist.setEmptyView(bar);
        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 5) {
                    list = (List<HashMap<String, String>>) msg.obj;
                    //String[] list_data={};
                    listItemAdapter = new SimpleAdapter(getContext(), list,
                            R.layout.list_item,
                            new String[]{"city","temp","wind"},
                            new int[]{R.id.title1, R.id.title2,R.id.title3});
                    mylist.setAdapter(listItemAdapter);
                }
                if (msg.what == 5) {
                    list = (List<HashMap<String, String>>) msg.obj;
                }
                super.handleMessage(msg);
            }
        };
        WeatherTask3 mytask=new WeatherTask3();
        mytask.setHandler(handler);
        Thread t = new Thread(mytask);
        t.start();
    }


}